<template>
    <div>
        <header-bar></header-bar>
        <div class="psd-box">
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="旧密码" prop="pass">
                    <el-input type="oldPassword" v-model.trim="ruleForm.oldPsd" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="checkPass">
                    <el-input type="newPassword" v-model.trim="ruleForm.newPsd" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码确认" prop="age">
                    <el-input type="password" v-model.trim="ruleForm.psd"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button class="bg-gray" @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
import headerBar from '../components/headerBar.vue'
import { updatePassword } from '../api/user/index'
export default {
    name: 'updatePassword',
    components: {
        headerBar
    },
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
            if (this.ruleForm.psd !== '') {
                this.$refs.ruleForm.validateField('psd');
            }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                oldPassword: '',
                newPassword: '',
                psd: ''
            },
            rules: {
                oldPassword: [{ validator: validatePass, trigger: 'blur' }],
                newPassword: [{ validator: validatePass, trigger: 'blur' }],
                psd: [{ validator: validatePass2, trigger: 'blur' }]
            }
        }
    },
    methods: {
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let params = {
                        txtAction:'updatePassword',
                        oldPassword: this.ruleForm.oldPassword,
                        newPassword: this.ruleForm.newPassword
                    }
                    updatePassword(params).then((res)=>{
                        if(res.data.code == '200') {
                            this.$message.success('密码修改成功');
                            this.$route.push({
                                path: '/login'
                            })
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs.ruleForm.resetFields();
        }
    }
}
</script>
<style lang="less" scoped>
.psd-box{
    width: 500px;
    margin: 0 auto;
    padding-top: 50px;
    /deep/.el-input{
        .el-input__inner{
            height: 32px;
        }
    }
    .el-button{
        width: 100px;
        height: 32px;
        padding: 0;
        line-height: 32px;
    }
    .bg-gray{
        background: #ddd;
    }
}
</style>